import { Component, Vue } from 'vue-property-decorator'
import InfoBlock from '@profile/util/info_block/Index.vue';
import axios from '@axios/user/index';
import { UserSearchRoots } from '@entity/user';

@Component({
    components: {
        InfoBlock
    }
})
export class FoundMeView extends Vue {

    /** 源数据 */
    private source: String = ''

    /** 被查找的权限 */
    private foundMe: UserSearchRoots = new UserSearchRoots();

    /**
     * 从服务器获取当前用户的被查找权限,
     * 并存为源数据
     */
    created() {
        axios.roots.querySearch(this.$store.getters.me().id).then(res => {
            this.foundMe = res;
            this.source = JSON.stringify(res);
        });
    }

    /**
     * 更新信息, 服务端更新成功后才更新本地
     */
    private save(): void {
        axios.roots.updateSearch(this.foundMe).then(res => {
            if (res) {
                this.source = JSON.stringify(this.foundMe);
            }
        });
    }

    /**
     * 判断是否进行了修改
     * 只需要将当前数据与源数据进行比较即可
     */
    private changed(): Boolean {
        return  this.source.isEmpty() || JSON.stringify(this.foundMe) === this.source;
    }

}